package com.ht.utils.htmlBean;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class Html {
    private String htmlHeader = "<!DOCTYPE html>\n" + "<html lang=\"zh-cn\"><body ";
    private List<Object> objs = new ArrayList<>();
    private float left;
    private float right;
    private float bottom;
    private float top;
    private float width;
    private float height;
    private String htmlFoot = "</body></html>";
    public void setLeft(float left) {
        this.width = this.width - left;
        this.left = left;
    }
    public void setRight(float right) {
        this.width = this.width - right;
        this.right = right;
    }
    public void setBottom(float bottom) {
        this.height = this.height - bottom;
        this.bottom = bottom;
    }
    public void setTop(float top) {
        this.height = this.height - top;
        this.top = top;
    }
    public void addObj(Object obj) {
        if (obj instanceof Table) {
            ((Table) obj).setWidth(((Table) obj).getWidth() / this.width);
        }
        this.objs.add(obj);
    }
    @Override
    public String toString() {
        StringBuilder html = new StringBuilder(htmlHeader);
        String style = "margin-top:" + top / 1440 * 72 / 0.75
                + "px;margin-right:" + right / 1440 * 72 / 0.75
                + "px;margin-left:" + left / 1440 * 72 / 0.75
                + "px;margin-bottom:" + bottom / 1440 * 72 / 0.75
                + "px;width:" + this.width / 1440 * 72 / 0.75
                + "px;height:" + this.height / 1440 * 72 / 0.75 + "px;";
        html.append(" style=\"").append(style).append("\">");
        html.append("<div id='pageStyle' style='display:none'>").append(style).append("</div>");
        if (objs != null && !objs.isEmpty()) {
            for (Object obj : objs) {
                if (obj instanceof PPR)
                    html.append(((PPR) obj).toHtml());
                if (obj instanceof Table)
                    html.append(((Table) obj).toHtml());
            }
        }
        html.append(htmlFoot);
        return html.toString();
    }

}

